<?php
App::uses('AppModel', 'Model');
/**
 * Usuario Model
 *
 * @property Perfil $Perfil
 */
class Usuario extends AppModel {
	var $situacaos = array(
		'A' => 'Ativo',
		'I' => 'Inativo'
	);
	
	/**
	 * Display field
	 *
	 * @var string
	 */
	public $displayField = 'nome';
	
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'login' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Preenchimento obrigatório',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'unique' => array(
					'rule' => array('isUnique'),
					'message' => 'Já existe um usuário com este login.',
					//'allowEmpty' => false,
					//'required' => false,
					//'last' => false, // Stop validation after this rule
					//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Preenchimento obrigatório',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'password' => array(
				'rule' => array('passwordIsValid'),
				'message' => 'A senha deve ter no mínimo 6 caracteres'
			)
		),
		'password_confirm' => array(
			'notempty' => array(
				'rule' => array('passwordIsEquals'),
			)
		),
		'perfil_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Perfil inválido',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'nome' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Preenchimento obrigatório',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				'message' => 'E-mail inválido',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'situacao' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Preenchimento obrigatório',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),		
		
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Perfil' => array(
			'className' => 'Perfil',
			'foreignKey' => 'perfil_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	
	/**
	 * Password hashing.
	 */
	public function beforeSave($options = array()) {
		if (isset($this->data['Usuario']['password'])) {
			$this->data['Usuario']['password'] = AuthComponent::password($this->data['Usuario']['password']);
		}
		
		return true;
	}
	
	
	public function passwordIsEquals($check)
	{
		if (!empty($this->data['Usuario']['password'])) {
			if ($this->data['Usuario']['password'] != $check['password_confirm']) {
				return __('As senhas não estão corretas.');
			}
		}
		
		return true;
	}
	
	
/**
 * Método para validar a senha do usuário
 *
 * @param $check array
 * @return bool 
 */
	public function passwordIsValid($check = null)
	{
		return (strlen($check['password']) >= 6);
	}
	
}
